const Behavior = require('@/behaviors/common'); // --混入
const Http = require('@/services/request')
const Toast = require('@/utils/toast')
const {
  login,
  logout
} = require('@/middleware/auth')
const appInstance=getApp();
const BASE_URL= appInstance.globalData.baseUrl;
Page({
  behaviors: [Behavior], // 引入Behavior
  data: {baseUrl: BASE_URL,
    userInfo: {},
    hasUserInfo: false,
    canIUseGetUserProfile: wx.getUserProfile ? true : false,
    quickNavList: [{
      num: '3',
      icon: '',
      url: '/sub-packages/user/pages/favorites/index',
      text: '我的收藏'
    }, {
      num: '16',
      icon: '',
      url: '/users/pages/follow/store/index',
      text: '关注店铺'
    }, {
      num: '15',
      icon: '',
      url: '',
      text: '浏览足迹'
    }, {
      num: '3',
      icon: '',
      url: '',
      text: '优惠券'
    }],
    orderStates: [{
      num: '',
      icon: '/assets/icons/order-01.png',
      url: '',
      text: '待支付'
    }, {
      num: '',
      icon: '/assets/icons/order-02.png',
      url: '',
      text: '待发货'
    }, {
      num: '',
      icon: '/assets/icons/order-03.png',
      url: '',
      text: '已发货'
    }, {
      num: '',
      icon: '/assets/icons/order-04.png',
      url: '',
      text: '已完成'
    }, {
      num: '',
      icon: '/assets/icons/order-05.png',
      url: '',
      text: '退款/售后'
    }],
    menusList: [{
      icon: '/assets/icons/menu-01.png',
      url: '/sub-packages/user/pages/address/list/index',
      text: '收货地址'
    }, {
      icon: '/assets/icons/menu-02.png',
      url: '/sub-packages/cart/pages/cart/index',
      text: '购物车'
    }, {
      icon: '/assets/icons/menu-03.png',
      url: '/sub-packages/user/pages/feedback/list/index',
      text: '意见反馈'
    }, {
      icon: '/assets/icons/menu-04.png',
      url: '',
      text: '帮助中心'
    }, {
      icon: '/assets/icons/menu-05.png',
      url: '',
      text: '在线客服'
    }, {
      icon: '/assets/icons/menu-06.png',
      url: '',
      text: '系统设置'
    }]
  },

  onLoad() {
    // 检查本地是否有用户信息
    const userInfo = wx.getStorageSync('userInfo');
    console.log(userInfo)
    if (userInfo) {
      this.setData({
        userInfo: userInfo,
        hasUserInfo: true
      });
    }
  },
  getUserLogin(e) {
    // --wx.getUserProfile接口废弃，改用直接登录的方式
   
    const pages = getCurrentPages();
    const currentPage = pages[pages.length - 1];
    const currentRoute = currentPage.route;
    const currentOptions = currentPage.options || {};
    console.log(`/pages/login/index?redirect=${encodeURIComponent(currentRoute)}&params=${encodeURIComponent(JSON.stringify(currentOptions))}`)
    wx.redirectTo({
      url: `/pages/login/index?redirect=${encodeURIComponent(currentRoute)}&params=${encodeURIComponent(JSON.stringify(currentOptions))}`
    });
  },
  getLoginOut() {
    let res = logout();
    console.log(res)
    this.setData({
      userInfo: {},
      hasUserInfo: false
    })
  },
  // 跳转到设置页面
  navigateToSetting() {
    wx.navigateTo({
      url: '/pages/setting/setting'
    });
  }
});